(function($){
    var client = new com.im.common.ajax();
    var loading = false;
    var planTypeList;
    var orgList;
    var projectList;
    var itemList;
    var ret = {};//返回的数据

    var methods = {
        init: function(options) {
            var o = $.extend({
                elmIdPlanTypeSel : "planTypeSel",
                elmIdUnitSel : "unitSel3",//组织单位下拉元素
                elmIdProjectSel : "projectSel",//规划工程下拉元素
                //elmIdItemSel : "itemSel",//实施项目下拉元素
                orgType : "",//初始化单位时，单位类型
                defaultContent : true ,//下拉框默认显示文本，true全部 false请选择
                callbackFunc: function(callBackType, param) {
                }
            }, options || {});

            var self = this;

            onInit(o);

            return this;
        },
        treeOnClickInner : function(self,opt){
            //加载数据
            return o.treeOnClick('1','test');
        }

    };

    function onInit(opt) {
        //初始化规划类型
        _renderPlanTypeList(opt);
        //初始化规划工程
        _initProjectSel(opt);
        //初始化组织
        _initOrgSel(opt);
        //初始化实施项目
        //_initItemSel(opt);
    };
    /**
     * 规划类型
     * @private
     */
    function _renderPlanTypeList(opt) {
        if(planTypeList == undefined){
            var result = client.callAsJson("projectInfoService/queryPlanProjClassConstList", {});
            if (!result.success) {
                toastr.error('服务器忙，规划工程类型查询失败！'+result.resultMsg);
                return;
            }
            planTypeList = result.result;
        }

        //规划类型列表添加元素
        if(opt.defaultContent){
            $("#"+opt.elmIdPlanTypeSel).append('<option value="">全部</option>');
        }else{
        	$("#"+opt.elmIdPlanTypeSel).append('<option value="">请选择</option>');
        }
        $.each(planTypeList, function (i, item) {
            $("#"+opt.elmIdPlanTypeSel).append('<option value="'+item.paramValValue+'">'+item.paramValName+'</option>');
        });
        //初始化事件
        $("#"+opt.elmIdPlanTypeSel).change(function () {
            //决定实施项目
            _filterProjects(opt);
            _returnFunc(1,opt);
        });
    };

    // function _initItemSel(opt){
    //     var param ={pageInfo:{pageNumber:1,pageSize:100}};
    //     //param.planProjName = $("#comp_item_project_name_input").val();
    //     //param.planProjClass = $("#comp_item_plan_type_sel").val();
    //     //param.impleProjName = $("#comp_item_item_name_input").val();
    //     param.sort = "start_dt";
    //     param.order = "desc";
    //     var result = client.callAsJson("projectInfoService/queryImpleProjList", param);
    //     if (result.success) {
    //         itemList = result.result.rows;
    //         //规划类型列表添加元素
    //         $("#"+opt.elmIdItemSel).append('<option value="">全部</option>');
    //         $.each(itemList, function (i, item) {
    //             $("#"+opt.elmIdItemSel).append('<option value="'+item.impleProjId+'">'+item.impleProjName+'</option>');
    //         });
    //         $('.select2').select2();
    //     } else {
    //         toastr.error(result.resultMsg);
    //         return;
    //     }
    //
    //     //初始化事件
    //     $("#"+opt.elmIdItemSel).change(function () {
    //         //决定实施项目
    //         _returnFunc(3,opt);
    //     });
    // }
    function _initOrgSel(opt){
        //加载数据
        var result = client.callAsJson("userService/queryOrgarizationsByCond", {"forOrgList": "",orgType : opt.orgType});
        if (result.success) {
            //规划类型列表添加元素
            $("#"+opt.elmIdUnitSel).append('<option value="">请选择建设单位</option>');
            if(result.result != undefined){
                orgList = result.result;
                $.each(orgList, function (i, item) {
                    $("#"+opt.elmIdUnitSel).append('<option value="'+item.orgId+'">'+item.orgName+'</option>');
                });
            }
        }else{
            toastr.error('服务器忙，查询失败！'+result.resultMsg);
        }

        //初始化事件
        $("#"+opt.elmIdUnitSel).change(function () {
            _returnFunc(2,opt);
        });
    }

    function _filterProjects(opt){
        var planProjClass = $("#"+opt.elmIdPlanTypeSel).val();
        ret.planProjClass = planProjClass;

        //过滤
        $("#"+opt.elmIdProjectSel).empty();
        if(opt.defaultContent){
            $("#"+opt.elmIdProjectSel).append('<option value="">全部</option>');
        }else{
        	$("#"+opt.elmIdProjectSel).append('<option value="">请选择</option>');
        }
        $.each(projectList, function (i, item) {
            if(planProjClass == '' || item.planProjClass == planProjClass){
                $("#"+opt.elmIdProjectSel).append('<option value="'+item.planProjId+'">'+item.planProjName+'</option>');
            }
        });
        // $('.select2').select2();
        // //初始化事件
        // $("#"+opt.elmIdItemSel).change(function () {
        //     //决定实施项目
        //     _returnFunc(3,opt);
        // });
    }

    function _initProjectSel(opt){
        var param ={pageInfo:{pageNumber:1,pageSize:100}};
        //param.planProjName = $("#comp_project_project_name_input").val();
        //param.planProjClass = $("#comp_project_plan_type_sel").val();
        param.sort = "start_dt";
        param.order = "desc";
        var result = client.callAsJson("projectInfoService/queryPlanProjList", param);
        if (result.success) {
            projectList = result.result.rows;
            //规划类型列表添加元素
            $("#"+opt.elmIdProjectSel).append('<option value="">全部</option>');
            $.each(projectList, function (i, item) {
                $("#"+opt.elmIdProjectSel).append('<option value="'+item.planProjId+'">'+item.planProjName+'</option>');
            });
            $('.select2').select2();
        } else {
            toastr.error(result.resultMsg);
            return;
        }
        $("#"+opt.elmIdProjectSel).change(function () {
            //决定实施项目
            _returnFunc(3,opt);
        });
    }

    function _returnFunc(callBackType,opt){
        ret.orgId = $("#"+opt.elmIdUnitSel).val();
        ret.orgName = $("#"+opt.elmIdUnitSel).find("option:selected").text();

        ret.planProjId = $("#"+opt.elmIdProjectSel).val();
        ret.planProjName = $("#"+opt.elmIdProjectSel).find("option:selected").text();

        ret.planProjClass = $("#"+opt.elmIdPlanTypeSel).val();
        ret.planProjClassName =  $("#"+opt.elmIdPlanTypeSel).find("option:selected").text();

        return opt.callbackFunc(callBackType,ret);
    };
    
    $.fn.plan2unit2projectCommpent = function(method) {

        if (methods[method] && method.charAt(0) != '_') {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' +  method + ' does not exist on jQuery.pagination');
        }
    };

})(jQuery);